package com.inepex.inechart.client.displayengine;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.inepex.inechart.client.cartesian.CartesianChartModuleGWT;

public class DisplayEngine {

	private class ViewportModuleMap{
	
		ViewportWidget vpw;
		CartesianChartModuleGWT csccm;
		
		public ViewportModuleMap(ViewportWidget vpw,
				CartesianChartModuleGWT csccm) {
			super();
			this.vpw = vpw;
			this.csccm = csccm;
		}		
	}
	
	protected final AbsolutePanel chartMainPanel;
	protected final LayerManager layerManager;
	protected int chartWidth;
	protected int chartHeight;
	protected ArrayList<ViewportModuleMap> vpMaps;
	
	public DisplayEngine(AbsolutePanel chartMainPanel) {
		super();
		this.chartMainPanel = chartMainPanel;
		this.layerManager = new LayerManager();
		vpMaps = new ArrayList<DisplayEngine.ViewportModuleMap>();
	}

	public AbsolutePanel getChartMainPanel() {
		return chartMainPanel;
	}

	public LayerManager getLayerManager() {
		return layerManager;
	}
	
	public ViewportWidget createViewportWidget(CartesianChartModuleGWT module){
		ViewportWidget vpw = new ViewportWidget(chartWidth, chartHeight);
		chartMainPanel.add(vpw);
		vpMaps.add(new ViewportModuleMap(vpw, module));
		return vpw;
	}
	
	public void updateViewportWidgetSizeAndPositionInsideChart(CartesianChartModuleGWT module){
		ViewportWidget vpw = null;
		for (ViewportModuleMap map : vpMaps) {
			if(map.csccm == module){
				vpw = map.vpw;
				break;
			}
		}
		if(vpw == null){
			return;
		}
		vpw.setSize(module.getCartesianAssist().getWidth(), module.getCartesianAssist().getHeight());
		vpw.setTileSize(module.getCartesianAssist().getWidth(), module.getCartesianAssist().getHeight());
		chartMainPanel.setWidgetPosition(vpw, (int)module.getLeftPadding(), (int) module.getTopPadding());
	}

	public int getChartWidth() {
		return chartWidth;
	}

	public int getChartHeight() {
		return chartHeight;
	}
	
	public void setChartSize(int width, int height){
		this.chartHeight = height;
		this.chartWidth = width;
		chartMainPanel.setPixelSize(width, height);
	}

	
}

